package com.hr.system.controller;

import java.util.List;

import com.hr.common.core.domain.entity.SysRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hr.common.annotation.Log;
import com.hr.common.enums.BusinessType;
import com.hr.system.domain.SysTenant;
import com.hr.system.service.ISysTenantService;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * 系统租户Controller
 * 
 * @author ruoyi
 * @date 2021-04-12
 */
@Controller
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController
{
    private String prefix = "system/tenant";

    @Autowired
    private ISysTenantService sysTenantService;

    @RequiresPermissions("system:tenant:view")
    @GetMapping()
    public String tenant()
    {
        return prefix + "/tenant";
    }

    /**
     * 查询系统租户列表
     */
    @RequiresPermissions("system:tenant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTenant sysTenant)
    {
        startPage();
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        return getDataTable(list);
    }

    /**
     * 导出系统租户列表
     */
    @RequiresPermissions("system:tenant:export")
    @Log(title = "系统租户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTenant sysTenant)
    {
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        return util.exportExcel(list, "tenant");
    }

    /**
     * 新增系统租户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统租户
     */
    @RequiresPermissions("system:tenant:add")
    @Log(title = "系统租户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTenant sysTenant)
    {
        return toAjax(sysTenantService.insertSysTenant(sysTenant));
    }

    /**
     * 修改系统租户
     */
    @GetMapping("/edit/{tenantId}")
    public String edit(@PathVariable("tenantId") Long tenantId, ModelMap mmap)
    {
        SysTenant sysTenant = sysTenantService.selectSysTenantById(tenantId);
        mmap.put("sysTenant", sysTenant);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统租户
     */
    @RequiresPermissions("system:tenant:edit")
    @Log(title = "系统租户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTenant sysTenant)
    {
        return toAjax(sysTenantService.updateSysTenant(sysTenant));
    }

    /**
     * 删除系统租户
     */
    @RequiresPermissions("system:tenant:remove")
    @Log(title = "系统租户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysTenantService.deleteSysTenantByIds(ids));
    }

    /**
     * 租户状态修改
     */
    @Log(title = "租户状态更改", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:tenant:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Long tenantId, String status)
    {
        SysTenant sysTenant = sysTenantService.selectSysTenantById(tenantId);
        sysTenant.setTenantStatus(status);
        sysTenantService.updateSysTenant(sysTenant);
        return AjaxResult.success();
    }
}
